package Operations;
import GUI.ResultData;
import Operations.Export.AExport;
import Operations.Export.Export;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * @author Martin
 * @version 1.0
 * @created 14-III-2012 13:12:31
 */
public class Operation extends AOperations {
    
    private final String[] EXTENSIONS = new String[]{"txt","csv"};
    //public SimpleFactory m_SimpleFactory;
    //public MonthDays m_MonthDays;
    //private ResultData result;

    public Operation(){
        super();  
        export = new Export();
    }

    @Override
    public void export(){
        //u exportu musime vybrat umisteni a nazev vytvareneho souboru (pres filechooser nebo neco takoveho)
        JFileChooser fileSaveDialog = new JFileChooser();
        fileSaveDialog.addChoosableFileFilter(new FileNameExtensionFilter("Dovolené přípony",EXTENSIONS));
        int option = fileSaveDialog.showSaveDialog(null);
        boolean done=true;
        
        switch(option)
        {
            case JFileChooser.APPROVE_OPTION:
                               
                if(fileSaveDialog.getSelectedFile().getAbsolutePath().toLowerCase().endsWith("csv"))
                {
                    done=export.doExport(dbData, fileSaveDialog.getSelectedFile().getAbsolutePath());
                }
                else if(fileSaveDialog.getSelectedFile().getAbsolutePath().toLowerCase().endsWith("txt"))
                {
                    //done=export.toTXT(dbData, fileSaveDialog.getSelectedFile().getAbsolutePath());
                }
                else
                {
                    //pokuď není zadána přípona vytvoří se csv
                    done=export.doExport(dbData, fileSaveDialog.getSelectedFile().getAbsolutePath() + ".csv");                                        
                }
                if(!done)update("Nepovedlo se exportovat vybraná data");
                else update("Data byla exportována");
                break;
            case JFileChooser.CANCEL_OPTION:
                break;
            default:
        }
        
    }
    


    /**
        * resultData vrati vysledek pro jednotlive pozadovane operace, SimpleFactory bude
        * provadet pocitani
        * 
        * @param to
        * @param from
        * @param resultData
        */
    @Override
    public void operationAll(Date from, Date to, ResultData resultData){
        GregorianCalendar calendar1 = new GregorianCalendar();
        calendar1.setTime(from);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        //System.out.println(calendar1.getTime().toString());
        GregorianCalendar calendar2 = new GregorianCalendar();
        calendar2.setTime(to);
        calendar2.set(Calendar.HOUR_OF_DAY, 23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        //System.out.println(calendar2.getTime().toString());
        //calendar.set(Calendar., month, date, hourOfDay, minute, second), month, date, hourOfDay, minute, second)
        
        dbData = db.getData(calendar1.getTime(), calendar2.getTime());
        //hledat i mezery od zacatku do konec (od kdy do kdy)- neporovnavat jen dva data mezi sebou ale i pocatecni a cilova data, 
        //pokud je dnesni datum, pak ten konec nemusime
        testGap(calendar1.getTime(), calendar2.getTime());
        makeOperation(resultData);
    }
    
    @Override
    public void operationAll(int month, int year, ResultData resultData) {
        //pomoci enumeratoru zjistime odkdy do kdy mame ziskat data prislusneho roku
        //pozor na unor a prestupny rok!!!
        
        GregorianCalendar calendar = new GregorianCalendar();
    
        calendar.set(year, month, 1, 0, 0);
        Date from = calendar.getTime();        
        //System.out.print(from.toString());
        
        calendar.set(year,month,calendar.getActualMaximum(calendar.DAY_OF_MONTH),23,59);
        Date to = calendar.getTime();
        //System.out.print(to.toString());

        dbData = db.getData(from, to);
        testGap(from,to);
        makeOperation(resultData);
    }

    @Override
    public void operationAll(ResultData resultData) {
        dbData = db.getData(new Date(0), new Date(Long.MAX_VALUE));
        testGap(dbData.get(0).getDate(),dbData.get(dbData.size()-1).getDate());
        makeOperation(resultData);
    }
    
    
}